<?php
include "./include/om_config.inc";
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $smarty = new SmartyWWW();

	$commoNameValuesArr[1]  = "CARDAMOM";
	$commoNameValuesArr[2]  = "COPPER";
	$commoNameValuesArr[3]  = "CPO";
	$commoNameValuesArr[4]  = "CRUDEOIL";
	$commoNameValuesArr[5]  = "GOLD";
	$commoNameValuesArr[6]  = "LEAD";
	$commoNameValuesArr[7]  = "MENTHAOIL";
	$commoNameValuesArr[8]  = "NATURALGAS";
	$commoNameValuesArr[9]  = "NICKEL";
	$commoNameValuesArr[10] = "POTATO";
	$commoNameValuesArr[11] = "SILVER";
	$commoNameValuesArr[12] = "ZINC";
	$commoNameValuesArr[13] = "BARLEYJPR";
	$commoNameValuesArr[14] = "CASTORSEED";
	$commoNameValuesArr[15] = "CHARJDDEL";
	$commoNameValuesArr[16] = "COCUDAKL";
	$commoNameValuesArr[17] = "DHANIYA";
	$commoNameValuesArr[18] = "GARGUMJDR";
	$commoNameValuesArr[19] = "GARSEDJDR";
	$commoNameValuesArr[20] = "GURCHMUZR";
	$commoNameValuesArr[21] = "JEERAUNJHA";
	$commoNameValuesArr[22] = "KAPASSRNR";
	$commoNameValuesArr[23] = "MAIZE";
	$commoNameValuesArr[24] = "POTATO";
	$commoNameValuesArr[25] = "PPRMLGKOC";
	$commoNameValuesArr[26] = "RMSEED";
	$commoNameValuesArr[27] = "STEELLONG";
	$commoNameValuesArr[28] = "SUGARM200";
	$commoNameValuesArr[29] = "SUGARS150";
	$commoNameValuesArr[30] = "SYBEANIDR";
	$commoNameValuesArr[31] = "SYOREFIDR";
	$commoNameValuesArr[32] = "TMCFGRNZM";
	$commoNameValuesArr[33] = "WHTSMQDELI";
 
  $filePath       = '';
  $fileName       = '';
  $expiryDate     = "";
  $expiryDateBc   = '';
  $bhavcopyDate   = '';
  $openPrice      = '';
  $highPrice      = '';
  $lowPrice       = '';
  $closePrice     = '';
  $openInterest   = '';
  $expiryMonthInt   = '';
  $previousClosingPrice   = '';
  
  if(isset($_POST['submitBtn']))
  {
    $uploaddir = dirname($_POST['filePath']);

    $uploadfile = $uploaddir . basename($_FILES['fileName']['name']);
    
    $target_path = './bhavcopiesfo';
    $target_path = $target_path ."/". basename( $_FILES['fileName']['name']);
    //$_FILES['fileName']['tmp_name']; // temp file
    
    $fileBaseName = basename($_FILES['fileName']['name']);

    // getting the extention

    $pos = strpos($fileBaseName,".",0);
    $ext = trim(substr($fileBaseName,$pos+1,strlen($fileBaseName))," ");
    
    if(move_uploaded_file($_FILES['fileName']['tmp_name'], $target_path)) 
    {
    	$row = 0;
      $handle = fopen($target_path, "r");
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
      {
	      if($row > 0 && strlen($data[6]) > 0)
	      {//We check $data[6] ... because if any error line is there in data file, then we do not want to handle that line.
	      	$exchange		        =	'NCDX';
	        $contractCode       = trim($data[0]);
	        $previousClosePrice =      $data[5];
	        $openPrice          =      $data[6];
	        $highPrice          =      $data[7];
	        $lowPrice           =      $data[8];
	        $closePrice         =      $data[9];
	        $openInterest       =      $data[14];
	        
	        $expiryDateStr      = strtoupper(substr($data[1],3,3));
	        $monthStr           = $pos;
	        
		    	$expiryDate       = trim($data[1]);
		    	$bhavcopyDate     = substr($fileBaseName,6,4)."-".substr($fileBaseName,0,2)."-".substr($fileBaseName,3,2);
		    	$firstSlashPos    = strpos(trim($data[1]),"/");
		    	$secondSlashPos   = strpos(trim($data[1]),"/",$firstSlashPos+1);

		    	$expiryDateDayWithout0  = substr(trim($data[1]),$firstSlashPos+1,$secondSlashPos - $firstSlashPos - 1);
		    	$expiryDateDay          = strlen($expiryDateDayWithout0) == 2 ? $expiryDateDayWithout0 : "0".$expiryDateDayWithout0;

		    	$expiryDateMonthWithout0  = substr(trim($data[1]),$firstSlashPos-2,$secondSlashPos - $firstSlashPos - 1);
		    	$expiryDateMonth  = strlen($expiryDateMonthWithout0) == 2 ? $expiryDateMonthWithout0 : "0".$expiryDateMonthWithout0;
		    	
		    	$expiryDateStrIns = substr(trim($data[1]),$secondSlashPos+1,4)."-".$expiryDateMonth."-".$expiryDateDay;

          
	        $insertBhavcopy = "INSERT INTO bhavcopyncdx (exchange,previousClosePrice,bhavcopyDate,contractCode,expiryDate,expiryDateBc,
	                                                     openPrice,highPrice,lowPrice,closePrice,openInterest)
	                           VALUES ('".$exchange."','".$previousClosePrice."','".$bhavcopyDate."','".$contractCode."','".$expiryDateStrIns."','".$expiryDate."',
	                                   '".$openPrice."','".$highPrice."','".$lowPrice."','".$closePrice."',".$data[14].")";
	        mysql_query($insertBhavcopy);
			  }
		    $row++;
      }
    } 
    else
      echo "There was an error uploading the file, please try again!";
  }
  $selectQuery = "SELECT * FROM general";
  $selectResult = mysql_query($selectQuery);
  if($selectRow = mysql_fetch_array($selectResult))
  {
    $filePath = $selectRow['filePath'];
    $fileName = $selectRow['fileName'];
  }
  
  $headMenu  = "<B>Tips : &nbsp;&nbsp;</B><A href='./txtFileF_O.php?exc=F_O'>SubmitF_O </A>&nbsp;&nbsp;&nbsp;";

  $smarty->assign("headMenu", $headMenu);
}
?>
<BODY><br>

<H1><CENTER><FONT color="red">TIPS</FONT></CENTER></H1><HR>

<CENTER>
  <FORM enctype="multipart/form-data" action="" name="form1" method="POST"> 
	<a href="index.php">Home</a>&nbsp;&nbsp;&nbsp;	<a href="bhavCopyListNcdx.php">List Ncdx</a>
	<br /><br />
	<input type="hidden" name="exc" value="F_O">
<INPUT size="60" type="hidden" name="filePath"><BR>
File Name: <INPUT name="fileName" type="file" onChange="document.form1.filePath.value=document.form1.fileName.value;">
<INPUT type="submit" name="submitBtn" value="Submit File"> 
</FORM>
</CENTER>